﻿//-----------------------------------------------------------------------
// <copyright file="WebsiteList.asmx.cs" company="CMSN Software">
//    Copyright © 2011  CMSN Software
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see http://www.gnu.org/licenses.
// </copyright>
//-----------------------------------------------------------------------
namespace JavaScriptSample
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Xml.Linq;

    /// <summary>
    /// web service for get websites details.
    /// </summary>
    [WebService(Namespace = "http://cmsnsoftware.blogspot.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class WebsiteList : System.Web.Services.WebService
    {
        /// <summary>
        /// URL of the xml file
        /// </summary>
        private const string WebsiteDetailsXml = "WebsiteDetails.xml";

        /// <summary>
        /// Xml node for Websites
        /// </summary>
        private const string WebsitesNode = "Websites";

        /// <summary>
        /// Xml node for Website
        /// </summary>
        private const string WebsiteNode = "Website";

        /// <summary>
        /// Xml node for Website title
        /// </summary>
        private const string TitleNode = "Title";

        /// <summary>
        /// Xml node for Website URL
        /// </summary>
        private const string UriNode = "Uri";

        /// <summary>
        /// Get Web sites details from XML file.
        /// </summary>
        /// <param name="websiteTitle">The web site title.</param>
        /// <returns>list of matching web sites</returns>
        [WebMethod]
        public List<Websites> WebsiteDetails(string websiteTitle)
        {
            List<Websites> websitesList = new List<Websites>();
            Websites websiteList = new Websites();

            XDocument websitesXml = XDocument.Load(Server.MapPath(WebsiteDetailsXml));
            var websites = from website in websitesXml.Descendants(WebsitesNode).Elements(WebsiteNode)
                           where (website.Element(TitleNode).Value.ToUpperInvariant().Contains(websiteTitle.ToUpperInvariant())
                           || website.Element(UriNode).Value.ToUpperInvariant().Contains(websiteTitle.ToUpperInvariant()))
                           select new
                           {
                               title = website.Element(TitleNode).Value,
                               url = website.Element(UriNode).Value
                           };

            foreach (var website in websites)
            {
                websiteList.Title = website.title;
                websiteList.NavigationLink = website.url;
                websitesList.Add(websiteList);
            }

            return websitesList;
        }
    }
}
